<!-- 单选按钮组 -->
<template>
  <div class="cd-check-group">
    <RadioGroup class="dib" v-model="computedValue" :size="size" v-bind="$attrs" @on-change="onChange">
      <Radio
        size="default"
        v-for="(item, key) in radioList"
        :label="item.value"
        :disabled="item.disabled || allDisabled"
        :key="key"
      >
        <span>{{ item.label }}</span>
      </Radio>
    </RadioGroup>
  </div>
</template>

<script lang="ts" setup>
import { apiFindDictList } from '@/api/common'
import { computed } from '@vue/reactivity'
import { onBeforeMount, ref, type Ref } from 'vue'
const props = defineProps({
  modelValue: {
    type: String,
    default: ''
  },
  list: {
    // 数据
    type: Array,
    default: () => []
  },
  size: {
    type: String,
    default: 'default'
  },
  allDisabled: {
    // 是否全部禁用
    type: Boolean,
    default: false
  },
  type: {
    // 字典类型
    type: String,
    default: ''
  },
  pId: {
    // 字典父级Id
    type: String,
    default: '-1'
  },
  pVal: {
    // 字典父级值
    type: String,
    default: ''
  }
})

const emits = defineEmits(['update:modelValue', 'on-change'])

const computedValue = computed({
  get() {
    return props.modelValue
  },
  set(val) {
    emits('update:modelValue', val)
  }
})

// 获取选项
interface IradioItem {
  label: string
  value: string
  disabled?: Boolean
}
const radioList: Ref<IradioItem[]> = ref([])
const getList = async () => {
  if (props.type) {
    const res = await apiFindDictList({
      type: props.type,
      parentId: props.pId,
      parentValue: props.pVal
    })
    const { isSuccess, data } = res
    if (isSuccess) {
      radioList.value = data
    }
  } else {
    radioList.value = props.list as IradioItem[]
  }
}
onBeforeMount(() => {
  getList()
})

// 选择回调
const onChange = (val: IradioItem) => {
  emits('on-change', val)
}
</script>
